'''
Created on Nov 19, 2010

@author: namikata
'''

import sys
import pygame
from pygame.locals import *
from sys import exit
from bk.pygame.ships.game.World import *
from bk.pygame.ships.game.base.Resources import *
from bk.pygame.ships.game.base.Keyboard import *
from bk.pygame.ships.game.base.Mouse import *
from bk.pygame.ships.game.Player import *
from bk.pygame.ships.game.Cursor import *
from bk.pygame.ships.game.UI_Player import *
from bk.pygame.ships.game.EffectList import *

main = None

class Main():
    def __init__(self):
        #start engine
        pygame.init()
        self.keyboard = Keyboard()
        self.mouse = Mouse()
        self.screen = None
        self.logfont = pygame.font.SysFont("Verdana", 12)
        self.world = World()
    
    def runDemo(self, resFile = "res.txt"):
        self.screen = pygame.display.set_mode((800,600),0,32)
        self.world.load_resources(resFile)
        self.world.fillimage = self.world.resources.back
                
        fullscreen = False
        debugging = False
        
        clock = pygame.time.Clock()
        #capture mouse if not debugging
        if not "-d" in sys.argv:
            pygame.mouse.set_visible(False)
            pygame.event.set_grab(True)

        
        fps = 0
        self.last_fps = 0
        total_elapsed = 0
        
        #player = Player(self.world,"player1",self.world.resources.dic["ship"],KCLASS_PLAYER,KSCLASS_PLAYERHUMAN,(400,400))
        player = Player(self.world,"player1",self.world.resources.ship_small,KCLASS_PLAYER,KSCLASS_PLAYERHUMAN,(400,400))
        player.load_armory("weapons.xml")
        cursor = Cursor(self.world,"cursor",self.world.resources.cursor,KCLASS_CURSOR,KSCLASS_CURSORPLAYER,(0,0))
        player.cursor = cursor
        player_ui = UI_Player(KCLASS_UI,KSCLASS_UIPLAYER,player, True)
        trail_effect = Effect_Trail(10,0.1)
        player.effect_library.add_effect(trail_effect)
        self.world.add_entity(player)
        self.world.add_entity(cursor)
        self.world.add_entity(player_ui)
                
        while True:           
            wheel_up = 0
            wheel_down = 0
            for event in pygame.event.get():
                if event.type == QUIT:
                    exit()
                elif event.type == MOUSEBUTTONDOWN:
                    if event.dict["button"] == 4: wheel_down += 1
                    elif event.dict["button"] == 5: wheel_up += 1
                    
            self.keyboard.update(pygame.key.get_pressed(), pygame.key.get_mods())
            self.mouse.update(pygame.mouse.get_pressed(),pygame.mouse.get_pos(),pygame.mouse.get_rel(), wheel_up, wheel_down)
                    
            if self.keyboard.get_pressed_key(K_ESCAPE) == 1:
                exit()
            if self.keyboard.get_pressed_key(K_f) == 1:
                fullscreen = not fullscreen
                self.screen = pygame.display.set_mode((800,600),HWSURFACE | FULLSCREEN | DOUBLEBUF if fullscreen else 0,32)
            if self.keyboard.get_pressed_key(K_o) == 1:
                debugging = not debugging
            if self.keyboard.get_pressed_key(K_p) == 1:
                self.world.paused = not self.world.paused
            if self.keyboard.get_pressed_key(K_u) == 1:
                self.world.show_ui = not self.world.show_ui
            if self.keyboard.get_pressed_key(K_KP_PLUS) == 1:
                self.world.time_scale = min(5,self.world.time_scale+0.1)
            if self.keyboard.get_pressed_key(K_KP_MINUS) == 1:
                self.world.time_scale = max(0.1,self.world.time_scale-0.1)
                    
            #seconds_elapsed = (clock.tick(60) / 1000.0)
            seconds_elapsed = (clock.tick() / 1000.0)
            transformed_time = seconds_elapsed * self.world.time_scale
        
            fps += 1
            total_elapsed += seconds_elapsed
            if total_elapsed > 1:
                self.last_fps = fps
                fps = 0
                total_elapsed -= 1
                    
            self.world.update(transformed_time, self.keyboard, self.mouse)
            self.world.render(self.screen)
                                      
            if debugging:
                self.debug()
                   
            if fullscreen:
                pygame.display.flip()
            else:

                pygame.display.update()
                
    def debug(self):
        player = self.world.get_by_name("player1")
        cursor = self.world.get_by_name("cursor")
        player_ui = self.world.get_by_name("player1_ui")
        step = 0
        if player:
            step = self.write(self.screen, self.logfont, "fps:%d" % (self.last_fps),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player.location:%r" % (player.location),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player.speed:%d" % (player.speed),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player.direction:%s" % (player.direction),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.cooldown:%r" % (player.armory.cooldown),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.energy:%r" % (player.armory.energy),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.weapon1:%r" % (player.armory.current_weapon_1.name),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.weapon2:%r" % (player.armory.current_weapon_2.name if player.armory.current_weapon_2 else u"None"),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.shooting:%r" % (player.armory.shooting),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.charging:%r" % (player.armory.charging),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "armory.charge:%r" % (player.armory.charge),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player.rotation:%r" % (player.rotation),True,(255,255,255),(0,0,0),step)
        if cursor:
            step = self.write(self.screen, self.logfont, "cursor.location:%s" % (cursor.location),True,(255,255,255),(0,0,0),step)
        step = self.write(self.screen, self.logfont, "paused:%r" % (self.world.paused),True,(255,255,255),(0,0,0),step)
        step = self.write(self.screen, self.logfont, "mouse pos. & delta:%r - %r" % (self.mouse.position,self.mouse.delta),True,(255,255,255),(0,0,0),step)
        step = self.write(self.screen, self.logfont, "mouse buttons:l:%r,r:%r,c:%r" % (self.mouse.get_pressed(LeftButton),self.mouse.get_pressed(RightButton),self.mouse.get_pressed(CenterButton)),True,(255,255,255),(0,0,0),step)
        step = self.write(self.screen, self.logfont, "entities:%r" % (len(self.world.entities)),True,(255,255,255),(0,0,0),step)
        if player_ui:
            step = self.write(self.screen, self.logfont, "player_ui.armor:%r" % (player_ui.armor),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player_ui.armor_max:%r" % (player_ui.armor_max),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player_ui.energy:%r" % (player_ui.energy),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player_ui.energy_max:%r" % (player_ui.energy_max),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player_ui.cooldown:%r" % (player_ui.cooldown),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "player_ui.cooldown_max:%r" % (player_ui.cooldown_max),True,(255,255,255),(0,0,0),step)
        step = self.write(self.screen, self.logfont, "world.energy_pool:%r" % (self.world.energy_pool),True,(255,255,255),(0,0,0),step)
        step = self.write(self.screen, self.logfont, "world.time_scale:%r" % (self.world.time_scale),True,(255,255,255),(0,0,0),step)
        
        if player and player.effect_library:
            step = self.write(self.screen, self.logfont, "player.effects:",True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "library_id:%r" % (player.effect_library.random_id),True,(255,255,255),(0,0,0),step)
            for effect in player.effect_library.eff.itervalues():
                step = self.write(self.screen, self.logfont, "effect[%d].duration:%r" % (effect.effect_class,effect.duration),True,(255,255,255),(0,0,0),step)
                step = self.write(self.screen, self.logfont, "effect[%d].time_elapsed:%r" % (effect.effect_class,effect.time_elapsed),True,(255,255,255),(0,0,0),step)
        if cursor and cursor.effect_library:
            step = self.write(self.screen, self.logfont, "library_id:%r" % (cursor.effect_library.random_id),True,(255,255,255),(0,0,0),step)
            step = self.write(self.screen, self.logfont, "cursor.effects:",True,(255,255,255),(0,0,0),step)
            for effect in cursor.effect_library.eff.itervalues():
                step = self.write(self.screen, self.logfont, "effect[%d].duration:%r" % (effect.effect_class,effect.duration),True,(255,255,255),(0,0,0),step)
                step = self.write(self.screen, self.logfont, "effect[%d].time_elapsed:%r" % (effect.effect_class,effect.time_elapsed),True,(255,255,255),(0,0,0),step)
        

    def write(self,screen, font, text, antialiasing, forecolor, backcolor, step):
        position = (0,step*15)
        if backcolor:
            screen.blit(font.render(text,antialiasing,forecolor,backcolor),position)
        else:
            screen.blit(font.render(text,antialiasing,forecolor),position)
        step += 1
        return step

if __name__ == '__main__':
    main = Main()
    main.runDemo()
    
    